<?php

/******************************************
 * File:			lib_parse.php
 * Version:			1.0.0
 * Last modified:	25/10/2009
 * Last modifier:	Hugo Lavigne
 *****************************************/

Function check_php_code($str, $color=FALSE)
{
	$i = 0;
	$str_len = strlen($str);
	$res = "";
	$flag = TRUE;
	
	while ($i < $str_len)
	{
		if ($flag == TRUE)
		{
			if (substr($str, $i, 5) == '<php>')
			{
				$i += 4;
				$flag = FALSE;
				$res .= "<div id=\"bcode\">";
			}
			else
				$res .= $str[$i];
		}
		elseif ($flag == FALSE)
		{
			if (substr($str, $i, 6) == '</php>')
			{
				$i += 6;
				$flag = TRUE;
				$res .= "</div>";
			}
			else
			{
				if ($color == TRUE)
				{
					if (substr($str, $i, 5) == '<?php')
					{
						$res .= "<font color=red>".htmlentities('<?php')."<br></font>";
						$i += 5;
					}
					if (substr($str, $i, 2) == '?>')
					{
						$res .= "<font color=red>?></font>";
						$i += 2;
					}
					elseif (substr($str, $i, 6) == 'class ')
					{
						$res .= "<font color=purple>class </font>";
						$i += 5;
					}
					if (substr($str, $i, 8) == 'function')
					{
						$res .= "<font color=purple>function</font>";
						$i += 8;
					}
					if (substr($str, $i, 8) == 'Function')
					{
						$res .= "<font color=purple>Function</font>";
						$i += 7;
					}
					elseif (substr($str, $i, 6) == 'public')
					{
						$res .= "<font color=red>public</font>";
						$i += 5;
					}
					elseif (substr($str, $i, 7) == 'private')
					{
						$res .= "<font color=red>private</font>";
						$i += 6;
					}
					elseif (substr($str, $i, 9) == 'protected')
					{
						$res .= "<font color=red>protected</font>";
						$i += 8;
					}
					elseif (substr($str, $i, 4) == 'trim')
					{
						$res .= "<font color=green>trim</font>";
						$i += 3;
					}
					elseif (substr($str, $i, 4) == 'strlen')
					{
						$res .= "<font color=green>trim</font>";
						$i += 3;
					}
					elseif (substr($str, $i, 2) == 'if')
					{
						$res .= "<font color=purple>if</font>";
						$i += 1;
					}
					elseif (substr($str, $i, 6) == 'elseif')
					{
						$res .= "<font color=purple>elseif</font>";
						$i += 5;
					}
					elseif (substr($str, $i, 4) == 'else')
					{
						$res .= "<font color=purple>else</font>";
						$i += 3;
					}
					elseif (substr($str, $i, 5) == 'while')
					{
						$res .= "<font color=purple>while</font>";
						$i += 4;
					}
					elseif (substr($str, $i, 3) == 'for')
					{
						$res .= "<font color=purple>for</font>";
						$i += 2;
					}
					elseif (substr($str, $i, 1) == '=')
						$res .= "<font color=darkgreen>=</font>";
					elseif (substr($str, $i, 2) == '++')
					{
						$res .= "<font color=darkgreen>++</font>";
						$i += 1;
					}
					elseif (substr($str, $i, 2) == '--')
					{
						$res .= "<font color=darkgreen>--</font>";
						$i += 1;
					}
					elseif (substr($str, $i, 2) == '+=')
					{
						$res .= "<font color=darkgreen>+=</font>";
						$i += 1;
					}
					elseif (substr($str, $i, 2) == '||')
					{
						$res .= "<font color=darkgreen>||</font>";
						$i += 1;
					}
					elseif (substr($str, $i, 2) == '&&')
					{
						$res .= "<font color=darkgreen>&&</font>";
						$i += 1;
					}
					elseif (substr($str, $i, 2) == '->')
					{
						$res .= "<font color=green>-></font>";
						$i += 1;
					}
					elseif (substr($str, $i, 5) == 'print')
					{
						$res .= "<font color=green>print</font>";
						$i += 4;
					}
					elseif (substr($str, $i, 5) == 'Print')
					{
						$res .= "<font color=green>Print</font>";
						$i += 4;
					}
					elseif (substr($str, $i, 4) == 'exit')
					{
						$res .= "<font color=red>exit</font>";
						$i += 3;
					}
					elseif (substr($str, $i, 5) == 'array')
					{
						$res .= "<font color=green>array</font>";
						$i += 4;
					}
					elseif (substr($str, $i, 1) == '{')
						$res .= "<font color=darkgreen>{</font>";
					elseif (substr($str, $i, 1) == '}')
						$res .= "<font color=darkgreen>}</font>";
					elseif (substr($str, $i, 1) == '(')
						$res .= "<font color=darkgreen>(</font>";
					elseif (substr($str, $i, 1) == ')')
						$res .= "<font color=darkgreen>)</font>";
					elseif ($str[$i] == '"')
					{
						$i++;
						$tmp = $i;
						$len = 0;
						while (substr($str, $tmp, 1) != '"')
						{
							$len++;
							$tmp++;
						}
						$save = "";
						$j = 0;
						$tmp = $i;
						while ($j < $len)
						{
							$save .= $str[$tmp];
							$j++;
							$tmp++;
						}
						$res .= "<font color=red>".'"'.$save.'"'."</font>";
						$i = $tmp;
					}
					elseif ($str[$i] == "'")
					{
						$i++;
						$tmp = $i;
						$len = 0;
						while (substr($str, $tmp, 1) != "'")
						{
							$len++;
							$tmp++;
						}
						$save = "";
						$j = 0;
						$tmp = $i;
						while ($j < $len)
						{
							$save .= $str[$tmp];
							$j++;
							$tmp++;
						}
						$res .= "<font color=red>'$save'</font>";
						$i = $tmp;
					}
					elseif (substr($str, $i, 2) == '/*')
					{
						$i++;
						$tmp = $i;
						$len = 0;
						while (substr($str, $tmp, 2) != '*/')
						{
							$len++;
							$tmp++;
						}
						$save = "";
						$j = 0;
						$tmp = $i;
						while ($j < $len)
						{
							$save .= $str[$tmp];
							$j++;
							$tmp++;
						}
						$res .= "<font color=orange>/$save*</font>";
						$i = $tmp;
					}
					elseif (substr($str, $i, 1) == '#')
					{
						$i++;
						$tmp = $i;
						$len = 0;
						while ((substr($str, $tmp, 1) != '\n') && ($tmp < $str_len))
						{
							$len++;
							$tmp++;
						}
						$save = "";
						$j = 0;
						$tmp = $i;
						while ($j < $len)
						{
							$save .= $str[$tmp];
							$j++;
							$tmp++;
						}
						$res .= "<font color=orange>#$save</font>";
						$i = $tmp;
					}
					else
						$res .= $str[$i];
				}
				else
					$res .= $str[$i];
			}
		}
		$i++;
	}
	return ($res);
}

?>